﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Csharp_Study
{

    //在泛型集合的接口中使用变体(C#)

    //public class Person
    //{
    //    public string FirstName { get; set; }
    //    public string LastName { get; set; }
    //}

    //public class Employee : Person { }

    //class Program
    //{
    //    // The method has a parameter of the IEnumerable<Person> type.  
    //    public static void PrintFullName(IEnumerable<Person> persons)
    //    {
    //        foreach (Person person in persons)
    //        {
    //            Console.WriteLine("Name: {0} {1}",
    //            person.FirstName, person.LastName);
    //        }
    //        Console.ReadKey();
    //    }

    //    static void Main(string[] args)
    //    {
    //        IEnumerable<Employee> employees = new List<Employee> {
    //           new Employee() {FirstName = "Michael", LastName = "Alexander"},
    //           new Employee() {FirstName = "Jeff", LastName = "Price"}
    //        };


    //        // You can pass IEnumerable<Employee>,
    //        // although the method expects IEnumerable<Person>.  

    //        PrintFullName(employees);

    //    }
    //}


    // Simple hierarchy of classes.  
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class Employee : Person { }

    // The custom comparer for the Person type  
    // with standard implementations of Equals()  
    // and GetHashCode() methods.  
    class PersonComparer : IEqualityComparer<Person>
    {
        public bool Equals(Person x, Person y)
        {
            if (Object.ReferenceEquals(x, y)) return true;
            if (Object.ReferenceEquals(x, null) ||
                Object.ReferenceEquals(y, null))
                return false;
            return x.FirstName == y.FirstName && x.LastName == y.LastName;
        }
        public int GetHashCode(Person person)
        {
            if (Object.ReferenceEquals(person, null)) return 0;
            int hashFirstName = person.FirstName == null
                ? 0 : person.FirstName.GetHashCode();
            int hashLastName = person.LastName.GetHashCode();
            return hashFirstName ^ hashLastName;
        }
    }

    class Program01
    {

        //static void Main(string[] args)
        //{
        //    List<Employee> employees = new List<Employee> {
        //       new Employee() {FirstName = "Michael", LastName = "Alexander"},
        //       new Employee() {FirstName = "Jeff", LastName = "Price"},
        //       new Employee() {FirstName = "Jeff", LastName = "Price"}
        //    };

        //    // You can pass PersonComparer,
        //    // which implements IEqualityComparer<Person>,  
        //    // although the method expects IEqualityComparer<Employee>.  

        //    IEnumerable<Employee> noduplicates =
        //        employees.Distinct<Employee>(new PersonComparer());

        //    foreach (var employee in noduplicates)
        //        Console.WriteLine(employee.FirstName + " " + employee.LastName);

        //    Console.ReadKey();
        //}
    }


}
